package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 仲夏夜
 * @since 2022-09-16
 */
@Service
@Slf4j
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Resource
    private EduVideoService eduVideoService;

    /**
     * 根据课程id查询课程大纲
     *
     * @param courseId
     * @return
     * @throws Exception
     */
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) throws Exception {
        log.info("根据课程id查询大纲-------------------------");
        List<ChapterVo> fina = new ArrayList<>();
        //1.根据课程id查询课程里所以的章节
        LambdaQueryWrapper<EduChapter> lambdaQueryChapter = new LambdaQueryWrapper();
        lambdaQueryChapter.eq(StringUtils.isNotBlank(courseId), EduChapter::getCourseId, courseId);
        List<EduChapter> chapterList = baseMapper.selectList(lambdaQueryChapter);
        //2.根据课程id查询课程里所以的小节
        LambdaQueryWrapper<EduVideo> lambdaQueryVideo = new LambdaQueryWrapper();
        lambdaQueryVideo.eq(StringUtils.isNotBlank(courseId), EduVideo::getCourseId, courseId);
        List<EduVideo> videoList = eduVideoService.list(lambdaQueryVideo);
        for (EduChapter eduChapter : chapterList) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            List<VideoVo> videoVoList = new ArrayList<>();
            for (EduVideo eduVideo : videoList) {
                VideoVo videoVo = new VideoVo();
                BeanUtils.copyProperties(eduVideo, videoVo);
                videoVoList.add(videoVo);
            }
            chapterVo.setChildren(videoVoList);
            fina.add(chapterVo);

        }
        log.info("结束查询课程大纲-----------------------------");
        return fina;
    }

    /**
     * 删除的方法删除的方法
     *
     * @param chapterId
     * @return
     * @throws Exception
     */
    @Override
    public boolean deleteChapter(String chapterId) throws Exception {
        LambdaQueryWrapper<EduVideo> lambda = new LambdaQueryWrapper();
        lambda.eq(StringUtils.isNotBlank(chapterId), EduVideo::getChapterId, chapterId);
        if (eduVideoService.count(lambda) > 0) {
            throw new GuliException(20001, "不能删除");
        } else {
            int delete = baseMapper.deleteById(chapterId);
            return delete > 0;
        }

    }

    /**
     * 根据课程id删除章节
     *
     * @param courseId
     */
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }
}
